
package sg.edu.nus.iss.cats.dataaccess.mysql;

import sg.edu.nus.iss.cats.model.*;
import java.sql.*;

import sg.edu.nus.iss.cats.dataaccess.DAOException;

public class CourseObjectHandler implements ObjectHandler {

    public String getTableName () {
        return DBConstants.coursesTableName;
    }

    public String getKeyParameterConstraint () {
        return DBConstants.c_courseId + "=?";
    }

    public void setKeyParameters (PreparedStatement statement, Object obj)
                   throws SQLException, DAOException {
        Course app = (Course) obj;
        statement.setInt (1, app.getCourseId());
    }

    public Object retrieveObject (ResultSet results)
                    throws SQLException {
        Course obj = new Course ();
        obj.setCourseId (results.getInt (DBConstants.c_courseId));
        obj.setEmployeeId (results.getString (DBConstants.c_employeeId));
        obj.setCourseName (results.getString (DBConstants.c_courseName));
        obj.setOrganiser (results.getString (DBConstants.c_organiser));
        obj.setFromDate (results.getDate (DBConstants.c_fromDate));
        obj.setToDate (results.getDate (DBConstants.c_toDate));
        obj.setFees (results.getFloat (DBConstants.c_fees));
        obj.setGstIncluded (results.getBoolean (DBConstants.c_gstincluded));
        obj.setJustification (results.getString (DBConstants.c_justification));
        obj.setStatus (results.getString (DBConstants.c_status));
        return obj;
    }

    public void insertKeys (ResultSet results, Object obj)
                   throws SQLException, DAOException {
        Course app = (Course ) obj;
        results.updateInt (DBConstants.c_courseId, app.getCourseId());
    }

    public void updateObject (ResultSet results, Object obj)
                   throws SQLException, DAOException {
        Course app = (Course) obj;
        results.updateString (DBConstants.c_employeeId, app.getEmployeeId());
        results.updateString (DBConstants.c_courseName, app.getCourseName());
        results.updateString (DBConstants.c_organiser, app.getOrganiser());
        results.updateDate (DBConstants.c_fromDate, new Date(app.getFromDate().getTime()));
        results.updateDate (DBConstants.c_toDate, new Date(app.getToDate().getTime()));
        results.updateFloat (DBConstants.c_fees, app.getFees());
        results.updateBoolean (DBConstants.c_gstincluded, app.getGstIncluded());
        results.updateString (DBConstants.c_justification, app.getJustification());
        results.updateString (DBConstants.c_status, app.getStatus());
    }

    public void completeObjectRetrieve (Object obj) throws DAOException {
        Course app = (Course) obj;
        SQLObjectManager mgr = new SQLObjectManager (
            new CourseEventObjectHandler ());
        mgr.setQueryConstraint (DBConstants.ce_courseId + "='" + app.getCourseId() + "'"
                + " ORDER BY " + DBConstants.ce_timestamp + " ASC");
        CourseEvent[] results
        = (CourseEvent[]) mgr.getObjects ();
        app.clearCourseEvents();
        app.setCourseEvents (results);
    }

    public Object[] getObjectArray (int length) {
        return new Course [length];
    }
}